﻿package CutLoad.transitions.cloud
{
	import CutLoad.Base;
	import CutLoad.cookies.Painter;
	
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 缓动传送效果 
	 * @author smallerbird 
	 * 
	 */	
	public class EasingDelivery extends Base
	{
		private var arrBase:Array;
		//当前移动的元素组
		private var arrShowBase:Array;
		//显示多少个
		//间隔
		private var space:Number;
		//显示出来的第几个元素
		private var playIndex:Number=0;
		private var showIndex:Number=0;
		//是否开始移动
		private var playType:Number=0;
		//视角宽度
		private var showWidth:Number=0;
		private var showHeight:Number=0;
		//此该移动的距离,相对距离
		private var temWidth:Number;
		//
		private var mcMask:Sprite;
		private var container:Sprite;
		//缓动参数
		private var offset:Number;
		//
		private var speed:Number;
		private var speed_ini:Number;
		//
		private var FunChange:Function;
		private var FunEnd:Function;
		private var FunEnterFrame:Function;
		/**
		 * 初始化
		 * @param arrBase 要显示的一系列base元素
		 * @param speed 移动速度，注：必须是10的倍数，一般设计为单个base元素宽的一半          ,因为算法上的限制，最终效果还要对"缓动系数"参数进行微调
		 * @param offset 缓动系数 注：因为算法上的限制，请结合移动速度进行微调
		 * @param space 一系列base元素放上去显示的间隔
		 * @param maskWidth 视窗宽度，默让为单个元素的宽度+间隔
		 * @param maskHeight 视窗高度,默让为单个元素的高度
		 * 
		 * 
		 */		
		public function EasingDelivery(arrBase:Array,speed:Number,offset:Number=0.75,space:Number=0,maskWidth:Number=0,maskHeight:Number=0):void
		{
			this.speed_ini=speed;
			//
			this.offset=offset;
			this.arrBase=arrBase;
			this.space=space;
			arrShowBase=new Array();
			container=new Sprite();
			this.addChild(container);
			//
			var baseTem:Base=arrBase[0];
			showHeight=baseTem.getShowHeight();
			showIndex=0;
			arrShowBase.push(baseTem);
			baseTem.show(container);
			showWidth+=baseTem.getShowWidth()+space;
			//
			if(maskWidth!=0) showWidth=maskWidth;
			if(maskHeight!=0) showHeight=maskHeight;
			//
			this.playType=0;
			temWidth=0;
			mcMask=Painter.getRect(showWidth,showHeight);
			this.addChild(mcMask);
			container.mask=mcMask;
			mcMask.visible=false;
			playIndex=0;
			//
			this.addEventListener(Event.ENTER_FRAME,enter_frame);
		}
		/**
		 * 重新设置速度和缓动系数 
		 * @param speed
		 * @param offset
		 * @return 
		 * 
		 */		
		public function reSetSpeedOffset(speed:Number,offset:Number):void{
			this.speed_ini=speed;
			this.offset=offset;
		}
		/**
		 * 设置每显示一张图时的回调方法 
		 * @param fun 格式为:function(me:EasingDelivery)
		 * 
		 */		
		public function setFunChange(fun:Function):void{
			FunChange=fun;
		}
		/**
		 * 设置每一张图移动完毕时的回调方法 
		 * @param fun 格式为:function(me:EasingDelivery)
		 * 
		 */		
		public function setFunEnd(fun:Function):void{
			FunEnd=fun;
		}
		/**
		 * 设置移动过程中循环调用的回调方法 
		 * @param fun 格式为:function(me:EasingDelivery)
		 * 
		 */		
		public function setFunEnterFrame(fun:Function):void{
			FunEnterFrame=fun;
		}
		public function next():void{
			if(playType!=0) return;
			//
			//这里是播放
			playIndex++;
			if(playIndex>arrBase.length-1){
				playIndex=arrBase.length-1;
				return;
			}
			//trace("playIndex:"+playIndex)
			speed=speed_ini;
			this.playType=1;
			temWidth=0;
			//
			//这里是补充
			//
			nextShow(showIndex)
		}
		public function pre():void{
			if(playType!=0) return;
			//这里是播放
			playIndex--;
			if(playIndex<0){
				playIndex=0;
				return;
			}
			//trace("playIndex:"+playIndex)
			this.playType=2;
			speed=speed_ini;
			temWidth=0;
			//这里是补充
			preShow()
			showIndex--;
		}
		/**
		 * 跳页 
		 * @param index
		 * 
		 */		
		public function go(index:Number):void{
			if(playType!=0) return;
			if(index<0) return;//index=0;
			if(index>arrBase.length-1) return; //index=arrBase.length-1
			if(playIndex==index) return;
			speed=speed_ini;
			temWidth=0;
			if(playIndex<index){
				playIndex=index;
				playType=1;
				nextShow(index)
			}else{
				playIndex=index;
				playType=2;
				preShow()
			}
			
		}
		/**
		 * 返回当前所处的第几个图片 
		 * @return 
		 * 
		 */		
		public function get value():Number{
			return playIndex;
		}
		override public function killMe():void{
			super.killMe();
		}
		/**
		 * 显示缓动效果的当前速度值 
		 * @return 
		 * 
		 */		
		public function get currentEasingSpeed():Number{
			return speed;
		}
		/**
		 * 获得当前显示的对象 
		 * @return 
		 * 
		 */		
		public function get currentItem():Base{
			var baseTem:Base=arrBase[value];
			return baseTem;
		}
		//
		private function enter_frame(e:Event):void{
			//trace("length::"+arrShowBase.length)
			if(this.playType==0) return;
			if(this.playType==1){
				speed*=offset;
				//右移
				for(var i:Number=0;i<arrShowBase.length;i++){
					var baseTem:Base=arrShowBase[i];
					baseTem.x-=speed;
				}
				temWidth+=speed;
				if(temWidth>=showWidth){
					var ca:Number=temWidth-showWidth
					for(var j:Number=0;j<arrShowBase.length;j++){
						baseTem=arrShowBase[j];
						baseTem.x+=ca;
					}
					//清除前一个
					var baseTem123:Base=arrShowBase.shift();
					baseTem123.killMe();
					playType=0;
					if(FunEnd!=null) FunEnd(this);
				}
				if(FunEnterFrame!=null) FunEnterFrame(this);
			}
			if(this.playType==2){
				//左移
				speed*=offset;
				for(i=0;i<arrShowBase.length;i++){
					baseTem=arrShowBase[i];
					baseTem.x+=speed;
				}
				temWidth+=speed;
				if(temWidth>=showWidth){
					ca=temWidth-showWidth
					for(i=0;i<arrShowBase.length;i++){
						baseTem=arrShowBase[i];
						baseTem.x-=ca;
					}
					//清除前一个
					var baseTem1234:Base=arrShowBase.pop();
					baseTem1234.killMe();
					playType=0;
					//
					if(FunEnd!=null) FunEnd(this);
				}
				if(FunEnterFrame!=null) FunEnterFrame(this);
			}
		}
		//
		private function nextShow(showIndexIndex:Number):void{
			var temLeftIndex:Number=playIndex;
			if(temLeftIndex>arrBase.length-1){
				return;
			}
			var baseTem:Base=arrBase[temLeftIndex];
			baseTem.x=arrShowBase[arrShowBase.length-1].x+arrShowBase[arrShowBase.length-1].getShowWidth()+space;
			baseTem.show(container);
			arrShowBase.push(baseTem);
			if(FunChange!=null) this.FunChange(this);
		}
		private function preShow():void{
			var temLeftIndex:Number=playIndex;
			if(temLeftIndex<0){
				return;
			}
			var baseTem:Base=arrBase[temLeftIndex];
			baseTem.x=arrShowBase[0].x-baseTem.getShowWidth()-space;
			baseTem.show(container);
			arrShowBase.unshift(baseTem);
			if(FunChange!=null) this.FunChange(this);
		}
	}
}